<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi as UserApi;
use Admin\Logic\GoodsLogic;

/**
 * 后台商品管理控制器
 * @author 明明 <zhumingming_12346@163.com>
 */
class GoodsController extends AdminController {

    /**
     * 后台商品列表管理
     * @author 明明 <634629786.qq.com>
     */
    public function index(){
        if(UID){
            $this->meta_title = '商品管理';
            $this->met_title = '商品列表管理';
            $info=M('Goods')->select();
            foreach($info as $k=>$v){
                $path=explode(',',$info[$k]['path']);
                $info[$k]['img']=M('Picture')->where(array('id'=>$path['1']))->getField('path');
                $info[$k]['cate_name']=M('Category')->where(array('id'=>$v['extend_cat_id']))->getField('title');
                }
            int_to_string($info);
            $this->assign('list',$info);
            $this->display();
        } else {
            $this->redirect('Public/login');
        }
    }
    /***
     * 添加商品页面
     */
    public function add(){
        $this->meta_title = '添加商品';
            /**分类 start**/
            $cate=M('Category')->where(array('pid'=>0))->field('id,title')->select();
            $this->assign('data',$cate);
            /**分类 end**/
            /**商品类型 start**/
            $goods=  M('goods_type')->select();
            $this->assign('goods',$goods);
            /**商品类型 end**/
        $this->display('edit');
    }

    /***
     * 商品提交地址
     * cat_id  分类id
     * extend_cat_id 扩展分类id
     * goods_type商品所属类型id，取值表goods_type的cat_id
     * spec_type 商品规格类型，取值表goods_type的cat_id
     * sales_sum   商品销量  7  55 506
     */
        public function commodity(){
            $Goods = D('Goods'); //
            if($_POST['goods_id']){  //编辑
                    if($Goods->create($_POST)){
                        if($_POST['cat_id']==0){
                            $this->error('请选择商品分类');
                        }
                        $arr=$_POST['img'];
                        array_multisort($arr);
                        $_POST['path']=implode(",",$arr);
                        $_POST['details']=serialize($_POST['details']);
                        $_POST['addtime']=time();
                        $goods_id = $insert_id = M('Goods')->where(array('id'=>$_POST['goods_id']))->save($_POST); // 写入数据到数据库
                        $Goods->afterSave($_POST['goods_id']);
                        if(empty($Goods)){
                            $this->error('商品编辑失败');
                        }else{
                            $this->success('商品编辑成功,正在跳转',U('index'));
                        }
                    }else{
                        $this->error($Goods->getError());
                    }
            }else{//插入
                    if($Goods->create($_POST)){
                            if($_POST['cat_id']==0){
                                $this->error('请选择商品分类');
                            }
                        $arr=$_POST['img'];
                        array_multisort($arr);
                        $_POST['path']=implode(",",$arr);
                        $_POST['details']=serialize($_POST['details']);
                        $_POST['addtime']=time();
                        $goods_id = $insert_id = $Goods->add($_POST); // 写入数据到数据库
                        $Goods->afterSave($goods_id);
                        if(empty($Goods)){
                            $this->error('商品添加失败');
                        }else{
                            $this->success('商品添加成功,正在跳转',U('index'));
                        }
                    }else{
                        $this->error($Goods->getError());
                    }
            }
        }


    /***
     * 修改商品
     */
    public function edit(){
        $this->meta_title = '编辑商品';
                $id=I('id');
        if(empty($id)){
            $this->error('参数错误',U('index'));
        }
        /**分类 start**/
        $cate=M('Category')->where(array('pid'=>0))->field('id,title')->select();

        /**分类 end**/
        /**商品类型 start**/
        $goods=  M('goods_type')->select();
        $this->assign('goods',$goods);
        $this->assign('data',$cate);
        /**商品类型 end**/
        $goods= M('Goods')->where(array('id'=>$id))->find();
         $title=  M('Category')->where(array('id'=>$goods['extend_cat_id']))->getField('title');
        $where['id']=array('in',$goods['path']);
        $rs['explode_photo_list']=$this->fieldAll('picture',$where,'path');
        $goods['details']=unserialize($goods['details']);
        $this->assign('datas',$rs);
        $this->assign('title',$title);
        $this->assign('info',$goods);
        $this->display('edit');
    }

    /***
     *  商品分类请求
     *
     */
    public function  cate(){
        $id=I('title');
        $arr=M('Category')->where(array('pid'=>$id))->field('id,title')->select();
        $this->ajaxReturn($arr);
    }
    /***
     * 商品规格请求
     *
     */
    public function ajaxGetSpecSelect(){
         $goods_id = I('goods_id') ? I('goods_id'): 0;
        $GoodsLogic = new GoodsLogic();
        //$_GET['spec_type'] =  13;
        $specList = D('Spec')->where("type_id = ".$_GET['spec_type'])->order('`id` desc')->select();
        foreach($specList as $k => $v)
            $specList[$k]['spec_item'] = M('SpecItem')->where("spec_id = ".$v['id'])->order('id')->getField('id,item'); // 获取规格项
        $items_id = M('SpecGoodsPrice')->where('goods_id = '.$goods_id)->getField("GROUP_CONCAT(`key` SEPARATOR '_') AS items_id");
        $items_ids = explode('_', $items_id);
        $this->assign('items_ids',$items_ids);
        $this->assign('specList',$specList);
        $this->display('ajax_spec_select');
    }
    /**
     * 动态获取商品规格输入框 根据不同的数据返回不同的输入框
     */
    public function ajaxGetSpecInput(){
        $GoodsLogic = new GoodsLogic();
        $goods_id = $_GET['goods_id'] ? $_GET['goods_id'] : 0;
        $str = $GoodsLogic->getSpecInput($goods_id ,$_POST['spec_arr']);
        exit($str);
    }
    /**
     *  删除商品
     */
    public function dele(){
                $id=I('id');
                if($id){
                    $path=M('Goods')->where(array('id'=>$id))->getField('path');
                    $map['id']=array('in',$path);
                    M('Picture')->where($map)->delete();
                    $goods=M('Goods')->where(array('id'=>$id))->delete();
                    $spec_goods=M('spec_goods_price')->where(array('goods_id'=>$id))->delete();
                    if($goods && $spec_goods){
                        $this->success('商品删除成功');
                    }else{
                        $this->error('商品删除失败');
                    }
                }else{
                    $this->error('参数错误');
                }
    }
    /***
     *   商品状态修改
     */
    public function change($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =  $id;
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('Goods', $map );
                break;
            case 'resumeuser':
                $this->resume('Goods', $map );
                break;

            /** 新品**/
            case 'newysuser':
                $this->newys('Goods', $map );
                break;
            case 'newynser':
                $this->newyn('Goods', $map );
                break;
            /** 新品**/

            /** 热销**/
            case 'hotysuser':
                $this->hotys('Goods', $map );
                break;
            case 'hotynuser':
                $this->hotyn('Goods', $map );
                break;

            /** 热销**/

            /** 置顶**/
            case 'topysuser':
                $this->topys('Goods', $map );
                break;

            case 'topynuser':
                $this->topyn('Goods', $map );
                break;
            /** 置顶**/


            case 'deleteuser':
                $this->delete('Goods', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }


    /**
     * 商品类型
     *
     */
    public function  type(){
        $data=M('GoodsType')->order('id desc')->select();
        int_to_string($data);
        $this->assign('list',$data);
        $this->meta_title='商品类型列表';
        $this->display();
    }
    /**
     * 新增商品类型
     */
    public function adtype(){
            if(IS_POST){
                if(empty($_POST['name'])){
                   $this->error('商品类型不能为空');
                }
                 $data['name'] =$_POST['name'];
                 $info= M('GoodsType')->add($data);
               if(empty($info)){
                   $this->error('商品类型添加失败');
               } else{
                   $this->success('商品类型添加成功',U('Goods/type'));
               }
            }else{
                $this->meta_title='新增商品类型';
                $this->display();
            }
    }

    /***
     * 修改商品类型
     */
    public function edtype(){
            $id=I('id');
        if($id){
                if(IS_POST){
                    if(empty($_POST['name'])){
                        $this->error('商品类型不能为空');
                    }
                    $data['name'] =$_POST['name'];
                    $info= M('GoodsType')->where(array('id'=>$_POST['id']))->save($data);
                    if(empty($info)){
                        $this->error('商品类型编辑失败');
                    } else{
                        $this->success('商品类型编辑成功',U('Goods/type'));
                    }
                }else{
                    $data=M('GoodsType')->where(array('id'=>$id))->find();
                    $this->assign('info',$data);
                    $this->meta_title='编辑商品类型';
                }
        }else{
            $this->error('请选择要修改的数据');
        }
        $this->display('adtype');
    }




    /**
     * 等级状态修改
     */
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('GoodsType', $map );
                break;
            case 'resumeuser':
                $this->resume('GoodsType', $map );
                break;
            case 'deleteuser':
                $this->delete('GoodsType', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }

    /**
     * 单个删除商品类型
     */
    public function  deltype(){
        $id= I('id');
        if(empty($id)){
            $this->error('参数错误,请重试！');
        }else{
           $del= M('GoodsType')->where(array('id'=>$id))->delete();
            if(empty($del)){
                $this->error('商品类型删除失败');
            }else{
                $this->success('商品类型删除成功',U('Goods/type'));
            }
        }

    }
}
